﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class ProductDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                HttpContext.Current.Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
            if (Request["ProductId"] == null)
            {
                Response.Redirect("ProductList.aspx");
            }
            int pid = Int32.Parse(Request["ProductId"]);
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/products.xml"));
            XmlNode product = doc.SelectSingleNode("//product[pid='" + pid + "']");
            if (product == null)
            {
                Response.Redirect("ProductList.aspx");
            }
            ProductDetailImage.ImageUrl = product.SelectSingleNode("pimage").InnerText;
            ProductDetailImage.AlternateText = product.SelectSingleNode("pname").InnerText;
            ProductDetailName.Text = product.SelectSingleNode("pname").InnerText;
            ProductDetailPrice.Text = product.SelectSingleNode("pprice").InnerText;
            ProductDetailTime.Text = product.SelectSingleNode("ptime").InnerText;
            ProductDetailInfo.Text = product.SelectSingleNode("pinfo").InnerText;
            AddToCart.CommandArgument = product.SelectSingleNode("pid").InnerText;
        }

        protected void AddToCart_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            Session["ProductId"] = btn.CommandArgument.ToString();
            Session["username"] = Session["username"];
            Response.Redirect("order.aspx");
        }

        protected void useradminButton_Click(object sender, EventArgs e)
        {
            Session["username"] = Request["username"];
            Response.Redirect("Default.aspx");
        }

        protected void AddProduct_Click(object sender, EventArgs e)
        {
            Session["username"] = Request["username"];
            Response.Redirect("AddProduct.aspx");
        }
    }
}